<?php

namespace App\Admin\Controllers;

use App\Models\Basics\Supplier;
use App\Models\Delivery\FBADelivery;
use App\Models\Inventory\TransferAudit;
use App\Models\Operation\ProfitSku;
use App\Models\Product\ProductAmazonSku;
use App\Models\Purchase\PurchaseOrder;
use App\Models\Reimburse\ReimburseApply;
use App\Models\Reimburse\ReimbursePaid;
use App\Models\Shipping\SSku;
use App\Models\Stocking\StockingApplyDetail;
use App\Models\Stocking\StockingSetDate;
use App\Models\Stocking\StockingSku;
use App\Models\Stocking\StockingSkuOther;
use App\Models\CustomerDelivery\CustomerDelivery;
use App\Util\ListPermission;
use Encore\Admin\Facades\Admin;
class Dashboard
{
    public static function pendingItem()
    {
        $data = [];
        //供应商资料审核【供应商资料】
        if(Admin::user()->can('supplier.approval')){
            $value = Supplier::query()->where('audit_status',0)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/basics/supplier?audit_status[]=0" style="color: red">'.$value.'</a> 件【供应商资料】等待您的审核！';
                array_push($data,$item);
            }
        }
        //利润表修改
        if(Admin::user()->can('amazon-sku.edit')){
            $query = ProfitSku::query()->where('status','<>',2);
            if(Admin::user()->isRole('sales-director')){
                $query = ListPermission::country($query);
            }elseif(!Admin::user()->isRole('sales-director') && Admin::user()->isRole('salesperson')){
                $query = ListPermission::seller($query);
            }
            $value = $query->count();
            if($value > 0){
                $item = '有 <a href="/xqc/operation/profit-sku?status[0]=0&status[1]=1" style="color: red">'.$value.'</a> 件【异常数据】等待您的修复！';
                $query1 = ProfitSku::query()->where('status',0);
                if(Admin::user()->isRole('sales-director')){
                    $query1 = ListPermission::country($query1);
                }elseif(!Admin::user()->isRole('sales-director') && Admin::user()->isRole('salesperson')){
                    $query1 = ListPermission::seller($query1);
                }
                $value1 = $query1->count();
                if($value1 > 0){
                    $item .= '<a href="/xqc/operation/profit-sku?status[]=0" style="color: red">'.$value1.'</a> 件【一般异常】';
                }
                $query2 = ProfitSku::query()->where('status',1);
                if(Admin::user()->isRole('sales-director')){
                    $query2 = ListPermission::country($query2);
                }elseif(!Admin::user()->isRole('sales-director') && Admin::user()->isRole('salesperson')){
                    $query2 = ListPermission::seller($query2);
                }
                $value2 = $query2->count();
                if($value2 > 0){
                    $item .= '<a href="/xqc/operation/profit-sku?status[]=1" style="color: red">'.$value2.'</a> 件【严重异常】';
                }
                array_push($data,$item);
            }
        }
        //泛欧产品异常记录
        if (Admin::user()->inRoles(['administrator','eu-sales-executive','eu-salesperson'])) {
            $value = ProductAmazonSku::query()->whereIn('times',[1,2,3])->count();
            if($value > 0){
                $item = '有 <a href="/xqc/products/amazon-sku?faneu_ex=2" style="color: red">'.$value.'</a> 件【泛欧异常记录】等待您的修复！';
                $value1 = ProductAmazonSku::query()->where('times',1)->count();
                if($value1 > 0){
                    $item .= '<a href="/xqc/products/amazon-sku?faneu_ex=3" style="color: red">'.$value1.'</a> 件【异常1】';
                }
                $value2 = ProductAmazonSku::query()->whereIn('times',[2,3])->count();
                if($value2 > 0){
                    $item .= '<a href="/xqc/products/amazon-sku?faneu_ex=4" style="color: red">'.$value2.'</a> 件【异常2&3】';
                }
                array_push($data,$item);
            }
        }
        //备货日期设定预警提示
        if(Admin::user()->can('stocking.set-date.edit')){
            $value = StockingSetDate::query()->where('status',0)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/stocking/set-date?status[]=0" style="color: red">'.$value.'</a> 件【设定备货日期记录已达预警】等待您的修复！';
                array_push($data,$item);
            }
        }
        //站内备货
        if(Admin::user()->can('stocking.sku-approval')){
            $value = StockingSku::query()->where('status',2)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/stocking/sku-approval" style="color: red">'.$value.'</a> 件【备货产品申请】等待您的审批！';
                array_push($data,$item);
            }
        }
        //站外统一备货
        if(Admin::user()->can('stocking.other-approval')){
            $value = StockingSkuOther::query()->where('status',1)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/stocking/other?status=1" style="color: red">'.$value.'</a> 件【备货申请单】等待您的审批！';
                array_push($data,$item);
            }
        }
        //提货需求
        if (Admin::user()->can('shipping.sku-approval')) {
            $query = SSku::query()->where('status', 2);
            $query = ListPermission::area($query);
            $value = $query->count();
            if($value > 0){
                $item = '有 <a href="/xqc/shipping/sku-approval" style="color: red">' . $value . '</a> 件【提货产品申请】等待您的审核！';
                array_push($data, $item);
            }
        }
        if (Admin::user()->can('shipping.sku.edit')) {
            $value = SSku::query()->where('status', 3)->where('seller_id', Admin::user()->id)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/shipping/sku?status=3&seller_id='.Admin::user()->id.'" style="color: red">' . $value . '</a> 件【提货产品申请】审核未通过！';
                array_push($data, $item);
            }
        }
        //采购计划
        if(Admin::user()->can('purchase.plan')) {
            $value = StockingApplyDetail::query()
                ->where('plan_status','<>',3)
                ->where('status', 1)
                ->distinct(['psku_id'])
                ->count('psku_id');
            if($value > 0){
                $item = '有 <a href="/xqc/purchase/plan" style="color: red">' . $value . '</a> 件【采购计划】等待您的处理！';
                array_push($data, $item);
            }
        }
        //采购下单
        if(Admin::user()->can('purchase.order')) {
            $value = PurchaseOrder::query()->where('status', 1)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/purchase/order?status=1" style="color: red">' . $value . '</a> 件【采购单】等待您的下单！';
                array_push($data, $item);
            }
        }
        //返款申请审核
        if (Admin::user()->can('reimburse.audit')) {
            $query = ReimburseApply::query()->where('apply_status', 2)->where('audit_status', 1);
            $query = ListPermission::country($query);
            $value = $query->count();
            if($value > 0){
                $item = '有 <a href="/xqc/reimburse/audit" style="color: red">' . $value . '</a> 件【返款申请】等待您的审核！';
                array_push($data, $item);
            }
        }
        //返款申请付款
        if (Admin::user()->can('reimburse.paid')) {
            $value = ReimbursePaid::query()->where('audit_status', 2)->where('paid_status', 1)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/reimburse/paid?audit_status=2&paid_status=1" style="color: red">' . $value . '</a> 件【返款申请】等待您的付款！';
                array_push($data, $item);
            }
        }
        //库存挪用审核
        if(Admin::user()->can('transfer-audit')) {
            $query = TransferAudit::query()->where('status',1);
            $query = ListPermission::oArea($query);
            $value = $query->count();
            if($value > 0){
                $item = '有 <a href="/xqc/inventory/transfer-audit" style="color: red">' . $value . '</a> 件【库存挪用申请】等待您的审核！';
                array_push($data, $item);
            }
        }

        //客户订单-多渠道发货需求
        if (Admin::user()->can('md.ship')) {
            $value = CustomerDelivery::query()->where('delivery_type',1)->where('status','<', 5)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/customer-delivery/delivery-fba" style="color: red">' . $value . '</a> 件【客户订单-FBA发货】等待您的发货！';
                array_push($data, $item);
            }
        }
        //客户订单-国内发货
        if(Admin::user()->can('not-fba.ship')) {
            $value = CustomerDelivery::query()->whereIn('delivery_type',[2,3])->where('status',1)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/customer-delivery/delivery-cn" style="color: red">' . $value . '</a> 件【客户订单-国内发货】等待您的备货处理！';
                array_push($data, $item);
            }
        }
        if(Admin::user()->can('not-fba.ship')) {
            $value = CustomerDelivery::query()->whereIn('delivery_type',[2,3])->where('status', 3)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/customer-delivery/delivery-cn?status=3" style="color: red">' . $value . '</a> 件【客户订单-国内发货】等待您的发货！';
                array_push($data, $item);
            }
        }

        if(Admin::user()->can('not-fba.logistics')) {
            $value = CustomerDelivery::query()->whereIn('delivery_type',[2,3])->where('status', 5)->where('logistics_status', 1)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/customer-delivery/delivery-cn?status=5&logistics_status=1" style="color: red">' . $value . '</a> 件【客户订单-国内发货】等待您更新物流！';
                array_push($data, $item);
            }
        }
        //FBA发货
        if(Admin::user()->can('fba.labels.marks')) {
            $value = FBADelivery::query()->where('status', 1)->where('seller_id',Admin::user()->id)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/delivery/fba?status=1&seller_id='.Admin::user()->id.'" style="color: red">' . $value . '</a> 件【FBA发货单】等待您上传标签！';
                array_push($data, $item);
            }
        }
        if(Admin::user()->can('fba.ship')) {
            $value = FBADelivery::query()->whereIn('status', [2, 3])->count();
            if($value > 0){
                $item = '有 <a href="/xqc/delivery/fba" style="color: red">' . $value . '</a> 件【FBA发货单】等待您的备货处理！';
                array_push($data, $item);
            }
        }
        if(Admin::user()->can('fba.labels.marks')) {
            $value = FBADelivery::query()->where('status', 4)->where('seller_id',Admin::user()->id)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/delivery/fba?status=4&seller_id='.Admin::user()->id.'" style="color: red">' . $value . '</a> 件【FBA发货单】等待您上传箱唛！';
                array_push($data, $item);
            }
        }
        if(Admin::user()->can('fba.ship')) {
            $value = FBADelivery::query()->where('status', 5)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/delivery/fba?status=5" style="color: red">' . $value . '</a> 件【FBA发货单】等待您的发货！';
                array_push($data, $item);
            }
        }
        if(Admin::user()->can('fba.logistics')) {
            $value = FBADelivery::query()->where('status', 6)->where('logistics_status', 1)->count();
            if($value > 0){
                $item = '有 <a href="/xqc/delivery/fba?status=6&logistics_status=1" style="color: red">' . $value . '</a> 件【FBA发货单】等待您更新物流！';
                array_push($data, $item);
            }
        }

        return view('dashboard.pending.item', compact('data'));
    }
}
